/**
 * Copyright (C) 2016 sharding-demo Project
 *               Author: Administrator
 *               Date: 2016年6月12日
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zdal.sharding.demo.router;

import java.util.Map;
import java.util.Random;

import com.zdal.sharding.core.router.table.AbstractRouter;
import com.zdal.sharding.demo.entity.T1;


/**
 * Name: SelectRouter.java
 * ProjectName: [sharding-demo]
 * Package: [com.zdal.sharding.demo.router.SelectRouter.java]
 * Description: TODO  
 * 
 * @since JDK1.7
 * @see
 *
 * Author: @author: Chris
 * Date: 2016年6月12日 下午4:51:15
 *
 * Update-User: @author
 * Update-Time:
 * Update-Remark:
 * 
 * Check-User:
 * Check-Time:
 * Check-Remark:
 * 
 * Company: 
 * Copyright: 
 */
public class SelectRouter implements AbstractRouter {

	@Override
	public String getInsertTableName(String tableName, Object paramObj) {
		return getTableName(tableName, paramObj);
	}

	@Override
	public String getUpdateTableName(String tableName, Object paramObj) {
		// TODO Auto-generated method stub
		return getTableName(tableName, paramObj);
	}

	@Override
	public String getDeleteTableName(String tableName, Object paramObj) {
		// TODO Auto-generated method stub
		return getTableName(tableName, paramObj);
	}

	@Override
	public String getSelectTableName(String tableName, Object paramObj) {
		// TODO Auto-generated method stub
		return getTableName(tableName, paramObj);
	}
	
	
	public static String getTableName( String tableName, Object paramObj ){
	    Integer id = 0;
	    if(paramObj instanceof T1){
	        T1 t= (T1) paramObj;
	        id = t.getId();
	    }
	    if(paramObj instanceof Integer){
	        id = (Integer)paramObj;
	    }
	    if(paramObj instanceof Map){
	        id = (Integer) ((Map)paramObj).get("id");
	    }
		int random =  (int)(id%2)+1;
		return String.format("%s_%s", tableName,random);
	}

	@Override
	public String getDbShardName(Object paramObj) {
		System.out.println(paramObj);
		int random =  (int)(Math.random() *2);
		return random+"";
	}
	
}
